package com.delivery.controller.user;

import com.delivery.dto.ShoppingCartAddDTO;
import com.delivery.entity.ShoppingCart;
import com.delivery.result.Result;
import com.delivery.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public Result addCart(@RequestBody ShoppingCartAddDTO dto) {
        log.info("addCart");
        shoppingCartService.addCart(dto);
        return Result.success();
    }

    /**
     * 查看购物车 当前用户的购物车
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> viewCart(){

        List<ShoppingCart> list = shoppingCartService.viewCart();
        return Result.success(list);
    }

    /**
     * 删除购物车中一个商品
     * @param dto
     * @return
     */
    @PostMapping("/sub")
    public Result deleteCart(@RequestBody ShoppingCartAddDTO dto){

        shoppingCartService.deleteCart(dto);
        return Result.success();
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result clearCart(){

        shoppingCartService.cleanCart();
        return Result.success();
    }
}
